package main

import (
	"fmt"
	"log"
	"os"
	"sort"

	"github.com/ahviplc/GoJustToolc/UConsole"
	"github.com/urfave/cli/v2"
)

func main() {
	var language string

	app := &cli.App{
		Flags: []cli.Flag{
			&cli.StringFlag{
				Name:        "lang, l",
				Aliases:     []string{"la"},
				Value:       "english",
				Usage:       "Language for the greeting",
				Destination: &language,
			},
			&cli.StringFlag{
				Name:  "config, c",
				Usage: "Load configuration from `FILE`",
			},
		},
		Commands: []*cli.Command{
			{
				Name:    "complete",
				Aliases: []string{"c", "c1", "c2"},
				Usage:   "complete a task on the list",
				Action: func(c *cli.Context) error {
					fmt.Println("com")
					return nil
				},
				After: func(context *cli.Context) error {
					fmt.Println("com af")
					return nil
				},
			},
			{
				Name:    "add",
				Aliases: []string{"a"},
				Usage:   "add a task to the list",
				Before: func(context *cli.Context) error {
					fmt.Println("add be")
					return nil
				},
				Action: func(c *cli.Context) error {
					fmt.Println("add")
					return nil
				},
				After: func(context *cli.Context) error {
					fmt.Println("add af")
					return nil
				},
			},
			{
				Name:    "greet",
				Aliases: []string{"gr"},
				Usage:   "greet",
				Before: func(context *cli.Context) error {
					fmt.Println("greet be")
					return nil
				},
				Action: func(c *cli.Context) error {
					name := "someone"
					if c.NArg() > 0 {
						name = c.Args().Get(0)
					}
					fmt.Println("c.Args():", c.Args())
					if language == "zh" {
						fmt.Println("你好", name)
					} else {
						fmt.Println("Hello", name)
					}
					return nil
				},
				After: func(context *cli.Context) error {
					fmt.Println("greet af")
					return nil
				},
			},
		},
	}

	sort.Sort(cli.FlagsByName(app.Flags))
	sort.Sort(cli.CommandsByName(app.Commands))

	err := app.Run(os.Args)
	if err != nil {
		log.Fatal(err)
	}

	UConsole.Log("...... cli loading ok ......")

	//NAME:
	//	app.exe - A new cli application
	//
	//USAGE:
	//	app.exe [global options] command [command options] [arguments...]
	//
	//	COMMANDS:
	//	add, a               add a task to the list
	//	complete, c, c1, c2  complete a task on the list
	//	greet, gr            greet
	//	help, h              Shows a list of commands or help for one command
	//
	//	GLOBAL OPTIONS:
	//	--config FILE             Load configuration from FILE
	//	--lang value, --la value  Language for the greeting (default: "english")
	//	--help, -h                show help (default: false)

	// 实际操作
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh gr LC
	//	greet be
	//	c.Args(): &[LC]
	//	你好 LC
	//	greet af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh greet SH
	//	greet be
	//	c.Args(): &[SH]
	//	你好 SH
	//	greet af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh add
	//	add be
	//	add
	//	add af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh complete
	//	com
	//	com af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh c
	//	com
	//	com af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh c1
	//	com
	//	com af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh c2
	//	com
	//	com af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>app.exe --la zh a
	//	add be
	//	add
	//	add af
	//
	//C:\_developSoftKu\ideaIU-2019.1.3.win\#GOPATHCodeKu\src\mygoclidemo>
}
